package com.usian.admin.filter;

import com.usian.model.admin.pojos.AdUser;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.AdminThreadLocalUtils;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Order(1)
@WebFilter(filterName = "AdminTokenFilter",urlPatterns = "/*")
public class AdminTokenFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //网关里面的鉴权token成功重新存入的请求头里面的userID     得到header中的信息
        String userId = request.getHeader("userId");
        if(userId != null){
            AdUser adUser = new AdUser();
            adUser.setId(Integer.valueOf(userId));
            AdminThreadLocalUtils.setUser(adUser);
        }
        filterChain.doFilter(request,response);
    }
}
